<?php

require_once('nusoap-modified/nusoap.php');

class Generator{
    var $prefix;
    var $wsdlUri;
    var $methodFieldsWithPriority;

    public function __construct($wsdlUri, $prefix = ''){
        $this->wsdlUri = $wsdlUri;
        $this->prefix = $prefix;
    }

    public function generate($directory = 'out'){
        $client = new nusoap_client($this->wsdlUri, 'wsdl');
        $client->loadWSDL();

        foreach($client->wsdl->bindings as $bindingName=>$value){
            $this->generateClassesForBinding($client, $bindingName);
        }
    }

    public function generateClassesForBinding($client, $binding){
        $operations = array();
        foreach($client->wsdl->bindings[$binding]['operations'] as $operation => $operationInformation){
            $operations[$operation] = array();
            foreach(array('input', 'output') as $direction){
                $xmlTypeId = $operationInformation[$direction]['parts']['parameters'];
                $type = $client->wsdl->getTypeDefFromType($xmlTypeId);
                $operations[$operation][$direction] = array();
                $operations[$operation][$direction]['elements'] = $type['elements'];

                $operations[$operation][$direction]['required_elements'] = array();
                $operations[$operation][$direction]['nonrequired_elements'] = array();
                $operations[$operation][$direction]['orderedMethodArguments'] = array();
                $operations[$operation][$direction]['orderedMethodArgumentsNoNulls'] = array();
                
                foreach($operations[$operation][$direction]['elements'] as $element){
                    if($element['minOccurs'] > 0){
                        $operations[$operation][$direction]['required_elements'][$element['name']] = $element;
                    } else {
                        $operations[$operation][$direction]['nonrequired_elements'][$element['name']] = $element;
                    }
                }

                foreach($operations[$operation][$direction]['required_elements'] as $required_element => $thing){

                    if(in_array($required_element, $this->methodFieldsWithPriority)){
                        array_unshift($operations[$operation][$direction]['orderedMethodArgumentsNoNulls'], '$' . $required_element);
                        array_unshift($operations[$operation][$direction]['orderedMethodArguments'], '$' . $required_element);
                    } else {
                        $operations[$operation][$direction]['orderedMethodArgumentsNoNulls'][] =  '$' . $required_element;
                        $operations[$operation][$direction]['orderedMethodArguments'][] = '$' . $required_element;
                    }

                }

                foreach($operations[$operation][$direction]['nonrequired_elements'] as $nonrequired_element => $thing){
                    if(in_array($nonrequired_element, $this->methodFieldsWithPriority)){
                        array_unshift($operations[$operation][$direction]['orderedMethodArgumentsNoNulls'], '$' . $nonrequired_element);
                        array_unshift($operations[$operation][$direction]['orderedMethodArguments'], '$' . $nonrequired_element);
                    } else {
                        $operations[$operation][$direction]['orderedMethodArgumentsNoNulls'][] =  '$' . $nonrequired_element;
                        $operations[$operation][$direction]['orderedMethodArguments'][] = '$' . $nonrequired_element . ' = null';
                    }
                }

                $operations[$operation][$direction]['allElementsRequiredFirst'] = array_merge($operations[$operation][$direction]['required_elements'], $operations[$operation][$direction]['nonrequired_elements']);

            }
        }
        include('templates/service.php');

        foreach($operations as $operationKey => $operation){
            ob_start();
            include('templates/example.php');
            $file_contents = ob_get_clean();
            $fileHandle = fopen(dirname(__FILE__) . '/out/examples/' . $operationKey . '.php', 'w');
            fwrite($fileHandle, $file_contents);
            fclose($fileHandle);
        }
    }
}